package com.couldsail.takeaway.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.couldsail.takeaway.constant.DefaultNumberConstant;
import com.couldsail.takeaway.context.BaseContext;
import com.couldsail.takeaway.mapper.AddressBookMapper;
import com.couldsail.takeaway.pojo.entity.AddressBook;
import com.couldsail.takeaway.pojo.vo.AddressBookVO;
import com.couldsail.takeaway.service.AddressBookService;
import com.couldsail.takeaway.utils.UniqueIdGenerator;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

@Slf4j
@Service
@RequiredArgsConstructor
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook> implements AddressBookService {
    private final AddressBookMapper addressBookMapper;
    @Override
    public void insert(AddressBook addressBook) {
        addressBookMapper.insert(addressBook);
    }

    @Override
    public void setDefault(AddressBook addressBook) {
        // 更新所有数据状态
        setOtherDefault();
        // 创建新的UpdateWrapper实例，用于第二次更新：将指定地址设置为默认
        UpdateWrapper<AddressBook> updateWrapperSpecific = new UpdateWrapper<>();
        updateWrapperSpecific.in("id", addressBook.getId())
                .set("is_default", DefaultNumberConstant.ADDRESS_IS_DEFAULT);
        addressBookMapper.update(null, updateWrapperSpecific);
    }

    @Override
    public void updateAddress(AddressBookVO addressBookVO) {
        if (addressBookVO.getIsDefault()){
            setOtherDefault();
        }
        UpdateWrapper<AddressBook> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",addressBookVO.getId());
        AddressBook addressBook = new AddressBook();
        BeanUtils.copyProperties(addressBookVO,addressBook);
        addressBook.setIsDefault(addressBookVO.getIsDefault()?DefaultNumberConstant.ADDRESS_IS_DEFAULT:DefaultNumberConstant.ADDRESS_NOT_DEFAULT);
        addressBookMapper.update(addressBook,updateWrapper);
    }

    @Override
    public void deleteAddress(Long id) {
        QueryWrapper<AddressBook> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        addressBookMapper.delete(queryWrapper);
    }

    @Override
    public AddressBookVO getDefaultAddress() {
        String current = BaseContext.getCurrentId();
        QueryWrapper<AddressBook> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",current).eq("is_default",DefaultNumberConstant.ADDRESS_IS_DEFAULT);
        AddressBook addressBook = addressBookMapper.selectOne(queryWrapper);
        AddressBookVO addressBookVO = new AddressBookVO();
        BeanUtils.copyProperties(addressBook,addressBookVO);
        return addressBookVO;
    }

    @Override
    public List<AddressBookVO> userList() {
        QueryWrapper<AddressBook> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", BaseContext.getCurrentId());
        List<AddressBook> addressBooks = addressBookMapper.selectList(queryWrapper);
        List<AddressBookVO> addressBookVOS = new ArrayList<>();
        addressBooks.forEach(item->{
            AddressBookVO addressBookVO = new AddressBookVO();
            BeanUtils.copyProperties(item,addressBookVO);
            addressBookVO.setIsDefault(Objects.equals(item.getIsDefault(), DefaultNumberConstant.ADDRESS_IS_DEFAULT));
            addressBookVOS.add(addressBookVO);
        });
        return addressBookVOS;
    }

    @Override
    public void insertAddressBook(AddressBookVO addressBookVO) {
        if (addressBookVO.getIsDefault()){
            setOtherDefault();
        }
        addressBookVO.setUserId(BaseContext.getCurrentId());
        AddressBook addressBook = new AddressBook();
        BeanUtils.copyProperties(addressBookVO,addressBook);
        addressBook.setId(UniqueIdGenerator.generateUniqueId());
        addressBook.setIsDefault(addressBookVO.getIsDefault()?DefaultNumberConstant.ADDRESS_IS_DEFAULT:DefaultNumberConstant.ADDRESS_NOT_DEFAULT);
        addressBookMapper.insert(addressBook);
    }
    public void setOtherDefault(){
        // 创建UpdateWrapper实例，用于第一次更新：将所有该用户的地址设置为非默认
        UpdateWrapper<AddressBook> updateWrapperAll = new UpdateWrapper<>();
        updateWrapperAll.eq("user_id", BaseContext.getCurrentId())
                .set("is_default", DefaultNumberConstant.ADDRESS_NOT_DEFAULT);
        addressBookMapper.update(null, updateWrapperAll);
    }
}
